package com.sise.config;

import com.sise.composition.PathInterceptor;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.*;

/**
 * @author zj
 */
@Configuration
public class MyWebMvcConfig implements WebMvcConfigurer {

    @Value("${upload.locationPicture}")
    private String locationPicture;

    @Value("${upload.pictureResourceHandler}")
    private String pictureResourceHandler;

    @Override
    public void addViewControllers(ViewControllerRegistry registry) {
        registry.addViewController("/").setViewName("/login");

        registry.addViewController("/bs_index-content").setViewName("/index-content");
        registry.addViewController("/bs_index-OrderInformation").setViewName("/index-OrderInformation");
    }

    /**
     * @param registry 设置本地地址映射
     */
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/static/**").addResourceLocations("classpath:/static/");
        registry.addResourceHandler(pictureResourceHandler).addResourceLocations("file:///" + locationPicture);
    }

    /**
     * @param registry 添加拦截器
     */
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(new PathInterceptor()).addPathPatterns("/**");
    }

    /**
     * @param registry 跨域请求
     */
    @Override
    public void addCorsMappings(CorsRegistry registry) {
        registry.addMapping("/**")
                .allowedOriginPatterns("*")
                .allowedMethods("POST", "GET", "PUT", "OPTIONS", "DELETE")
                .maxAge(3600)
                .allowCredentials(true);
    }
}
